package cn.mw.cmdb.entity;

import cn.mw.cmdb.param.ImportContextParam;
import cn.mw.components.mongodb.entity.CriteriaOpsType;
import cn.mw.components.mongodb.entity.MPropertyTypeLong;
import cn.mw.microMonitorCommon.entity.cmdb.ModelExportDataInfoParam;
import cn.mw.microMonitorCommon.entity.cmdb.MwImportResultParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.mongodb.core.query.Criteria;

import java.util.HashMap;
import java.util.Map;

import static cn.mw.cmdb.util.ValConvertUtil.longValueConvert;

@Slf4j
public class PropertyTypeLong extends PropertyType<PropertyTypeLong, Long> {
    public static final String ID = MPropertyTypeLong.ID;

    private MPropertyTypeLong mPropertyTypeLong = new MPropertyTypeLong();

    public PropertyTypeLong() {
        setId(ID);
        setName("长整型");
        setValueTip("请输入数字");
    }

    @Override
    public boolean validateValue(Object value, PropertyTypeRef propertyTypeRef) throws Exception {
        if (value instanceof String) {
            Long.parseLong(value.toString());
        } else {
            Long ret = (Long) value;
        }
        return true;
    }

    public Long convertValue(Object value) {
        if (value instanceof String) {
            return Long.parseLong(value.toString());
        }
        return (Long) value;
    }

    @Override
    public Criteria doCriteria(CriteriaOpsType opsType, String name, Object value) {
        return mPropertyTypeLong.doCriteria(opsType, name, value);
    }

    @Override
    public CriteriaOpsType getDefaultCriteriaOpsType() {
        return CriteriaOpsType.is;
    }

    @Override
    public PropertyTypeLong newObj() {
        return new PropertyTypeLong();
    }


    @Override
    public void refresh() {

    }

    @Override
    public Object getDataValue(ImportContextParam contextParam) {
        MwImportResultParam resultParam = new MwImportResultParam();
        String message = "";
        ModelExportDataInfoParam importParam = contextParam.getParam();
        //属性名称
        String tableName = importParam.getTableName();
        Object cellValue = importParam.getCellValue();
        Map<String, Object> matchMap = new HashMap<>();
        importCheckInfo(resultParam, contextParam, matchMap);
        log.info("Long类型::");
        resultParam.setCellValue(longValueConvert(cellValue));
        resultParam.setMessage(message);
        return resultParam;
    }
}
